/**
 * NSDate+GTExtensionTest.m
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */

#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
#import "NSDate+GTExtension.h"

@interface NSDate_GTExtensionTest : SenTestCase {
}
@end


@implementation NSDate_GTExtensionTest

- (void)testDateFromYearMonthDay {
  NSDate *date = [NSDate dateWithYear:2001 month:9 day:23];

  NSCalendar *calendar = [NSCalendar currentCalendar];
  unsigned unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
  NSDateComponents *comps = [calendar components:unitFlags fromDate:date];  
  
  STAssertEquals([comps year], 2001, @"Wrong year");
  STAssertEquals([comps month], 9, @"Wrong month");
  STAssertEquals([comps day], 23, @"Wrong month");
  STAssertEquals([comps hour], 0, @"Wrong hour");
  STAssertEquals([comps minute], 0, @"Wrong minute");
  STAssertEquals([comps second], 0, @"Wrong second");
}

- (void)testDateFromHourMinuteSecond {
  NSDate *date = [NSDate dateWithHour:20 minute:25 second:45];
  
  NSCalendar *calendar = [NSCalendar currentCalendar];
  unsigned unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
  NSDateComponents *comps = [calendar components:unitFlags fromDate:date];  
  
  STAssertEquals([comps year], [[NSDate date] year], @"Wrong year");
  STAssertEquals([comps month], [[NSDate date] month], @"Wrong month");
  STAssertEquals([comps day], [[NSDate date] day], @"Wrong month");
  STAssertEquals([comps hour], 20, @"Wrong hour");
  STAssertEquals([comps minute], 25, @"Wrong minute");
  STAssertEquals([comps second], 45, @"Wrong second");
}

- (void)testAddedProperties {
  NSDate *date = [NSDate dateWithYear:2001 month:9 day:23 hour:20 minute:25 second:45];
  STAssertEquals(date.year, 2001, @"Wrong year");
  STAssertEquals(date.month, 9, @"Wrong month");
  STAssertEquals(date.day, 23, @"Wrong month");
  STAssertEquals(date.hour, 20, @"Wrong hour");
  STAssertEquals(date.minute, 25, @"Wrong minute");
  STAssertEquals(date.second, 45, @"Wrong second");
}

- (void)testCompareOnlyYearMonthDay {
  NSDate *date = [NSDate dateWithYear:2010 month:4 day:20 hour:15 minute:20 second:45];  
  NSDate *sameDateWithDifferentTime = [NSDate dateWithYear:2010 month:4 day:20 hour:10 minute:10 second:15];
  NSDate *differentDate = [NSDate dateWithYear:2009 month:5 day:17 hour:15 minute:20 second:45];  
  STAssertEquals([date compareOnlyYearMonthDay:date], NSOrderedSame, @"Comparing same date should return NSOrderedSame");
  STAssertEquals([date compareOnlyYearMonthDay:sameDateWithDifferentTime], NSOrderedSame, @"Comparing with a date with same year, month a day should return NSOrderedSame");
  STAssertEquals([date compareOnlyYearMonthDay:differentDate], NSOrderedDescending, @"Comparing with a date with different year, month a day shouldn't return NSOrderedSame");
}

- (void)testCompareOnlyHourMinuteSecond {
  NSDate *date = [NSDate dateWithYear:2010 month:4 day:20 hour:15 minute:20 second:45];
  NSDate *sameDateWithDifferentDay = [NSDate dateWithYear:2010 month:6 day:10 hour:15 minute:20 second:45];
  NSDate *differentTime = [NSDate dateWithYear:2010 month:4 day:20 hour:16 minute:25 second:47];
  STAssertEquals([date compareOnlyHourMinuteSecond:date], NSOrderedSame, @"Comparing same date should return NSOrderedSame");
  STAssertEquals([date compareOnlyHourMinuteSecond:sameDateWithDifferentDay], NSOrderedSame, @"Comparing with a date with same hour, minute and second should return NSOrderedSame");
  STAssertEquals([date compareOnlyHourMinuteSecond:differentTime], NSOrderedAscending, @"Comparing with a date with different hour, minute and second shouldn't return NSOrderedSame");
}

- (void)testConversationOfSecondsInHoursMinutesSecondsAndViceversa {
  NSDate *expected = [NSDate dateWithHour:0 minute:0 second:0];
  NSDate *actual = [NSDate dateWithTimeFromSeconds:0];
  STAssertEqualObjects(actual, expected, @"Wrong conversation seconds -> hours:minutes:seconds");
  STAssertEquals([actual timeInSeconds], 0.0, @"Wrong conversation hours:minutes:seconds -> seconds");
  
  expected = [NSDate dateWithHour:0 minute:1 second:0];
  actual = [NSDate dateWithTimeFromSeconds:SECONDS_IN_ONE_MINUTE];
  STAssertEqualObjects(actual, expected, @"Wrong conversation seconds -> hours:minutes:seconds");
  STAssertEquals([actual timeInSeconds], SECONDS_IN_ONE_MINUTE, @"Wrong conversation hours:minutes:seconds -> seconds");
  
  expected = [NSDate dateWithHour:1 minute:0 second:0];
  actual = [NSDate dateWithTimeFromSeconds:SECONDS_IN_ONE_HOUR];
  STAssertEqualObjects(actual, expected, @"Wrong conversation seconds -> hours:minutes:seconds");
  STAssertEquals([actual timeInSeconds], SECONDS_IN_ONE_HOUR, @"Wrong conversation hours:minutes:seconds -> seconds");
  
  expected = [NSDate dateWithHour:1 minute:1 second:0];
  actual = [NSDate dateWithTimeFromSeconds:SECONDS_IN_ONE_HOUR + SECONDS_IN_ONE_MINUTE];
  STAssertEqualObjects(actual, expected, @"Wrong conversation seconds -> hours:minutes:seconds");
  STAssertEquals([actual timeInSeconds], SECONDS_IN_ONE_HOUR + SECONDS_IN_ONE_MINUTE, @"Wrong conversation hours:minutes:seconds -> seconds");
  
  expected = [NSDate dateWithHour:1 minute:1 second:13];
  actual = [NSDate dateWithTimeFromSeconds:SECONDS_IN_ONE_HOUR + SECONDS_IN_ONE_MINUTE + 13];
  STAssertEqualObjects(actual, expected, @"Wrong conversation seconds -> hours:minutes:seconds");
  STAssertEquals([actual timeInSeconds], SECONDS_IN_ONE_HOUR + SECONDS_IN_ONE_MINUTE + 13, @"Wrong conversation hours:minutes:seconds -> seconds");
}

@end
